package com.hulei.memcached.xmemcached;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.MemcachedClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class XMemcachedController {

	@Resource
	private MemcachedClient memcachedClient;

	@RequestMapping("/XMemcachedIndex")
	public String XMemcachedIndex() {
		try {
			//新增操作
			memcachedClient.set("XMemcachedKeyOne",0,"张三");
			System.out.println((String)memcachedClient.get("XMemcachedKeyOne"));

			//删除操作
			memcachedClient.delete("XMemcachedKeyOne");
			System.out.println((String)memcachedClient.get("XMemcachedKeyOne"));

			//设置存活时间
			memcachedClient.set("XMemcachedKeyTwo",1,"李四");
			Thread.sleep(2000);
			System.out.println((String)memcachedClient.get("XMemcachedKeyTwo"));

			//更新操作
			memcachedClient.set("XMemcachedKeyThree",0,"王五");
			System.out.println((String)memcachedClient.get("XMemcachedKeyThree"));
			memcachedClient.set("XMemcachedKeyThree",0,"王五他儿子");
			System.out.println((String)memcachedClient.get("XMemcachedKeyThree"));

		} catch (Exception e) {
			log.error("XMemcachedIndex error",e);
		}
		return "xMemcachedIndex";
	}
}
